package com.srd.phone.mvp.presenters;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.mvp.interfaces.IBaseView;
import com.srd.phone.utils.Constants;

import java.util.HashMap;
import java.util.Map;


public class UtilPresenter extends BasePresenter {

    public UtilPresenter(RequestQueue requestQueue, IBaseView iView0) {
        super(requestQueue,iView0);
    }

    public void requestData(final String url, final int page) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("page", String.valueOf(page));
                map.put("pagesize", String.valueOf(Constants.PAGE_SIZE));
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    public void requestData(final String url, final int page, final String stat) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("page", String.valueOf(page));
                map.put("pagesize", String.valueOf(Constants.PAGE_SIZE));
                map.put("stat", stat);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    public void accountDetail(final String url, final int id) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("id", String.valueOf(id));
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    public void noParams(final String url) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        });

        request.setTag(url);
        requestQueue.add(request);
    }


    public void singleParam(final String url, final String keyName, final String value) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put(keyName, value);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    public void mapParams(final String url, final Map<String, String> map){

        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);

    }

}
